package com.drcurd.controller;

import com.drcurd.entity.Brand;
import com.drcurd.service.BrandService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌控制器
 */
@RestController
@RequestMapping("/api/brands")

public class BrandController {
    
    @Autowired
    private BrandService brandService;
    
    /**
     * 分页查询品牌列表
     */
    @GetMapping("/page")
    public Map<String, Object> getBrandList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> result = new HashMap<>();
        try {
            PageInfo<Brand> pageInfo = brandService.getBrandList(page, size);
            result.put("success", true);
            result.put("data", pageInfo.getList());
            result.put("total", pageInfo.getTotal());
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据条件搜索品牌
     */
    @GetMapping("/search")
    public Map<String, Object> searchBrands(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> result = new HashMap<>();
        try {
            com.drcurd.dto.BrandQueryDTO queryDTO = new com.drcurd.dto.BrandQueryDTO();
            queryDTO.setName(name);
            queryDTO.setPageNum(page);
            queryDTO.setPageSize(size);
            
            PageInfo<Brand> pageInfo = brandService.getBrandListByCondition(queryDTO);
            result.put("success", true);
            result.put("data", pageInfo.getList());
            result.put("total", pageInfo.getTotal());
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询所有品牌（不分页）
     */
    @GetMapping
    public Map<String, Object> getAllBrands() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Brand> brands = brandService.getAllBrands();
            result.put("success", true);
            result.put("data", brands);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据ID查询品牌
     */
    @GetMapping("/{id}")
    public Map<String, Object> getBrandById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Brand brand = brandService.getBrandById(id);
            if (brand != null) {
                result.put("success", true);
                result.put("data", brand);
                result.put("message", "查询成功");
            } else {
                result.put("success", false);
                result.put("message", "品牌不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 添加品牌
     */
    @PostMapping
    public Map<String, Object> addBrand(@RequestBody Brand brand) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = brandService.addBrand(brand);
            if (success) {
                result.put("success", true);
                result.put("message", "添加成功");
            } else {
                result.put("success", false);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 更新品牌
     */
    @PutMapping("/{id}")
    public Map<String, Object> updateBrand(@PathVariable Long id, @RequestBody Brand brand) {
        brand.setId(id);
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = brandService.updateBrand(brand);
            if (success) {
                result.put("success", true);
                result.put("message", "更新成功");
            } else {
                result.put("success", false);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除品牌
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteBrand(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = brandService.deleteBrand(id);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
} 